<?php

/**
 * token 用数据进行缓存
 */

class cache_token
{   
	public static $model=null;
	public static $config = array(
			'DB_NAME' => 'manage',//数据库名
			'DB_HOST'=>'localhost',//数据库主机，一般不需要修改
			'DB_USER'=>'root',//数据库用户名
			'DB_PWD'=>'happyview',//数据库密码
			'DB_CACHE_ON' => true,//是否开启数据库缓存，true开启，false不开启
			'DB_CACHE_PATH' => './data/db_cache_wx/',//数据库查询内容缓存目录，地址相对于入口文件，一般不需要修改
	);
    
    /**
     * 更新token 数据
     */
    public  static  function    updateToken($paramArr = array()){
    	if(self::$model==null)
    	{
    		self::$model=new cpModel(self::$config);//实例化数据库模型类
    	}
        $options = array(
        	'appId'			=>'',
            'type'           => '',
            'token'          => '',      #数据
            'exTime'         =>''
		);
		if (is_array($paramArr)) $options = array_merge($options, $paramArr);
		extract($options);
        
        if(empty($appId) || empty($type)  ||  empty($token)  ||  empty($exTime)){
            return false;
        }
        
        $data  =  self::checkToken(array(
        			'appId' =>$appId,
                    'type'  =>$type
        ));
        
        $curTime  = time();
        $endTime = $curTime+$exTime;
        $flag     = false;
        if($data){
        	$sql = "UPDATE  `h_wx_token` SET `appId`='{$appId}', `token` = '{$type}',`token`='{$token}',`addTime`={$curlTime},`endTime`={$endTime},`exTime`={$exTime} WHERE `id` ={$data}";
			self::$model->query($sql);
        }else{
        	$sql = "INSERT INTO `h_wx_token` (`appId`,`type`,`token`,`addTime`,`endTime`,`exTime`) VALUES ('{$appId}','{$type}','{$token}',{$curTime},{$endTime},{$exTime});";
			self::$model->query($sql);
			$flag = self::$model->insert_id();
       	 	return $flag;
	    }
    }
    
    /**
     * 检查是否有这个token
     * 
     */
    
    public  static  function   checkToken($paramArr){
    	if(self::$model==null)
    	{
    		self::$model=new cpModel(self::$config);//实例化数据库模型类
    	}
        $options = array(
        	'appId'			=>'',
            'type'           => '',
		);
		if (is_array($paramArr)) $options = array_merge($options, $paramArr);
		extract($options);
        
        if(empty($type)||empty($appId)){ return false;}
        
        $sql = "SELECT `id` FROM `h_wx_token` WHERE `appId`= '{$appId}'	AND `type`= '{$type}'";
		$list = self::$model->query($sql);
		if($list){
			return $list[0];
		}else{
			return false;
		}
        
    }
    
    /**
     * 检查是否有这个token
     * 
     */
    
    public  static  function   getToken($paramArr = array()){
    	if(self::$model==null)
    	{
    		self::$model=new cpModel(self::$config);//实例化数据库模型类
    	}
        $options = array(
        	'appId'	=>'',
            'type' => '',
		);
		if (is_array($paramArr)) $options = array_merge($options, $paramArr);
		extract($options);
        if(empty($type)||empty($appId)){ return false;}
        $sql = "SELECT * FROM `h_wx_token` WHERE `appId`= '{$appId}' AND`type`= '{$type}'";
		$list = self::$model->query($sql);
		if($list){
			return $list[0];
		}else{
			return false;
		}
    }
    
    /**
     * 获得 token 数据
     */
    
    public  static  function  get($paramArr  = array()){
    	if(self::$model==null)
    	{
    		self::$model=new cpModel(self::$config);//实例化数据库模型类
    	}
    	$options = array(
        	'appId'=>'',
            'type' => '',
		);
		if (is_array($paramArr)) $options = array_merge($options, $paramArr);
		extract($options);
        if(empty($type)||empty($appId)){ return false;}
        $sql = "SELECT * FROM `h_wx_token` WHERE `appId`='{$appId}' AND `type`= '{$type}' AND `endTime` >".time();
        $list = self::$model->query($sql);
    	if($list){
			return $list[0];
		}else{
			return false;
		}
        
    }
}